package com.villaday.idf.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.villaday.idf.AppContext;
import com.villaday.idf.base.greendao.DaoMaster;
import com.villaday.idf.base.greendao.DaoSession;
import com.villaday.idf.base.greendao.VDao;
import com.villaday.idf.model.H;
import com.villaday.idf.model.Resources_filtersEntity;
import com.villaday.idf.model.V;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by tudou on 2018/10/17.
 */

public class DbVUtils {

    private static DbVUtils db;
    private final static String dbName = "v_db";
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;


    public DbVUtils() {
        context = AppContext.getInstance();
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }


    /**
     * 获取单例
     *
     * @return
     */
    public static DbVUtils getInstance() {
        if (db == null) {
            synchronized (DbVUtils.class) {
                if (db == null) {
                    db = new DbVUtils();
                }
            }
        }
        return db;
    }


    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }


    public void save(V info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        VDao downInfoDao = daoSession.getVDao();
        downInfoDao.insert(info);
    }

    public void saveAll(final List<Resources_filtersEntity.BodyEntity.VEntity> info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        final VDao downInfoDao = daoSession.getVDao();

        if (info == null || info.isEmpty()) {
            return;
        }
        downInfoDao.getSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < info.size(); i++) {
                    downInfoDao.insertOrReplace(new V(info.get(i)));
                }
            }
        });

    }

    public void update(V info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        VDao downInfoDao = daoSession.getVDao();
        downInfoDao.update(info);
    }

    public void deleteDowninfo(V info) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        VDao downInfoDao = daoSession.getVDao();
        downInfoDao.delete(info);
    }

    public void deleteAll() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        VDao downInfoDao = daoSession.getVDao();
        downInfoDao.deleteAll();
    }

    public V queryDownBy(long Id) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        VDao downInfoDao = daoSession.getVDao();
        QueryBuilder<V> qb = downInfoDao.queryBuilder();
        qb.where(VDao.Properties.Id.eq(Id));
        List<V> list = qb.list();
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public List<V> queryDownAll() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        VDao downInfoDao = daoSession.getVDao();
        QueryBuilder<V> qb = downInfoDao.queryBuilder();
        return qb.list();
    }

    public List<V> queryWithSomethingAll(String something) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        VDao downInfoDao = daoSession.getVDao();
        List<V> items = downInfoDao.queryBuilder()
                .where(VDao.Properties.Name.like("%"+something + "%"))
                .list();
        return items;
    }
}
